"""
注册功能
    1。需要用户名和密码以及确认密码
    2。注册时如果用户名已经存在，则不能再次注册
登录功能
    1。需要使用已经注册的用户信息登录
    2。密码输入错误3次后，锁定账户信息(不能再使用这个账户进行登录操作)
"""

# 定义变量存放用户名和密码
usernamelist = []
userpswdlist = []
binduserlist = []

# 获取所有用户的用户名和密码
with open('./test/userinfo.txt', 'a+') as fp:
    fp.seek(0)
    userinfos = fp.readlines()
    for i in userinfos:
        i = i.strip()
        username = i.split(':')[0]
        password = i.split(':')[1]
        usernamelist.append(username)
        userpswdlist.append(password)

# 获取被锁定的用户名
def getbinduserlist():
    with open('./test/binduser.txt', 'a+') as fp:
        fp.seek(0)
        for i in fp.readlines():
            i = i.strip()
            binduserlist.append(i)


getbinduserlist()

# 注册函数
def register():
    iscontinue = True
    while iscontinue:
        username = input('请输入用户名：')
        if username in usernamelist:
            print('用户名已存在，请重新输入')
        else:
            while True:
                password = input('请输入密码：')
                if len(password) < 3:
                    print('密码格式不正确，请重新输入')
                else:
                    repassword = input('请再次输入密码：')
                    if password == repassword:
                        with open('./test/userinfo.txt', 'a+') as fp:
                            fp.write(f'{username}:{password}\n')
                        print('注册成功！')
                        iscontinue = False
                        break
                    else:
                        print('两次输入密码不一致，请重新输入')

# 登录函数
def login():
    iscontinue = True
    while iscontinue:
        username = input('请输入用户名：')
        if username in usernamelist:
            if username in binduserlist:
                print('该用户已被锁定，请联系相关人员')
            else:
                userindex = usernamelist.index(username)
                truepassword = userpswdlist[userindex]
                n = 3
                while n >= 0:
                    if n == 0:
                        print('给你机会你不中用啊，该账户已被锁定')
                        with open('./test/binduser.txt', 'a+') as fp:
                            fp.write(f'{username}\n')
                        getbinduserlist()
                    else:
                        password = input('请输入密码：')
                        if password == truepassword:
                            print('登录成功！')
                            iscontinue = False
                            break
                        else:
                            if n-1 > 0:
                                print(f'密码输入错误，你还有{n-1}次机会')
                    n -= 1

        else:
            print('不存在的用户名！请重新输入')


type = input('请输入要进行的操作：\n0 注册\n1 登录\n')

if type == '0':
    register()
else:
    login()
